﻿<script type="text/javascript">
    function addCondition() {
        var subID = parseInt($('#currentSubid').val()) + 1;
        var len = document.getElementById("ConditionKey").options.length;
        if (subID < len) {
            var subDiv = $('#subCondition0');
            subDiv = subDiv.clone(true);
            subDiv.attr("style", "padding-left:9px");
            subDiv.attr("id", "subCondition" + subID);
            subDiv.children("#SubConditionType0").attr("name", "SubConditionType" + subID);
            subDiv.children("#SubConditionType0").attr("id", "SubConditionType" + subID);
            subDiv.children("#SubConditionKey0").attr("name", "SubConditionKey" + subID);
            subDiv.children("#SubConditionKey0").attr("id", "SubConditionKey" + subID);
            subDiv.children("#SubConditionValue0").attr("name", "SubConditionValue" + subID);
            subDiv.children("#SubConditionValue0").attr("id", "SubConditionValue" + subID);

            subDiv.children("#SubConditionQuery0").attr("name", "SubConditionQuery" + subID);
            subDiv.children("#SubConditionQuery0").attr("id", "SubConditionQuery" + subID);
            $('#subCondition' + (subID - 1)).after(subDiv[0].outerHTML);
            $('#currentSubid').attr("value", subID);
            document.getElementById("SubConditionKey" + subID)[subID].selected = true; //选中项
            $("#SubConditionValue" + subID).attr("maxlength", 50);
            //相似词检索
            $("#SubConditionValue" + subID).keyup(smartKeyup);

        }
    }

    function delCondition() {

        var subID = parseInt($('#currentSubid').val());
        if (subID > 0) {
            $('#subCondition' + subID).remove();
            $('#currentSubid').attr("value", subID - 1);
        }
    }
    //复制原有查询条件
    function copyContion() {
        $("#beforeConditionValue").val($("#ConditionValue").val());
        $("#beforeConditionKey").val($("#ConditionKey").val());
        $("#beforeQueryValue").val($("#SubConditionQuery").val());
        var subID = parseInt($('#currentSubid').val());//获取当前条件数量
        if (subID > 0) {
            $("#beforeContion").html("");
            for (var i = 1; i <= subID; i++) {
                var beforeType = "beforeConditionType" + i;//新的id
                var conType = $('#' + 'SubConditionType' + i).val();//值
                $("#beforeContion").append("<input id=" + beforeType + " name=" + beforeType + " type=\"text\" value='" + conType + "' />");

                var beforeKey = "beforeConditionKey" + i;//新的id
                var conKey = $('#' + 'SubConditionKey' + i).val();//值
                $("#beforeContion").append("<input id=" + beforeKey + " name=" + beforeKey + " type=\"text\" value='" + conKey + "' />");

                var beforeValue = "beforeConditionValue" + i;//新的id
                var contValue = $('#' + 'SubConditionValue' + i).val();//值
                $("#beforeContion").append("<input id=" + beforeValue + " name=" + beforeValue + " type=\"text\" value='" + contValue + "' />");

                var beforeQuery = "beforeQueryValue" + i;//新的id
                var contQuery = $('#' + 'SubConditionQuery' + i).val();//值
                $("#beforeContion").append("<input id=" + beforeValue + " name=" + beforeQuery + " type=\"text\" value='" + contQuery + "'/>");

            }

        }

    }
    //点击检索按钮
    function searchResult(m) {
        var checked = [];
        if (m == -1)//点击检索时，将原有条件复制，分页时不做处理
        {
            copyContion();
            $('#pageIndexNow').val('1');
            $('.talkDivLi .f-checkbox:checkbox:checked').each(function () {
                checked.push($(this).val());
            }); //点击检索时取弹出框内勾选的数据库

        }
        else {
            $('.d-checkbox:checkbox:checked').each(function () {
                checked.push($(this).val());
            }); //下一页，列表切换时取左侧勾选的数据库

        }
        if (checked.length == 0) {
            messageAlert('请先选择数据库！');
            return;
        }
        $("#checkDatabaseID").val(checked);
        $.ajax({
            type: "POST",
            url: "/MultiSearch/Search",
            data: $('#formSearch').serialize(),
            beforeSend: function () { $("#loading").show(); $("#loadingleft").show() },
            success: function (msg) {
                $("#loading").hide();
                $("#loadingleft").hide()
                $("#reslultdiv").html("");
                $("#reslultdiv").html(msg);
                if (m == -1)//点击检索并检索成功时，才刷新左侧视图
                {
                    refreshLeft();
                }
            },
            error: function (e) {
                $("#loading").hide();
                $("#loadingleft").hide()
                $(".wrap-sideL").html("<h3 class=\"wrap-l-t\">已选数据来源</h3><div class=\"wrap-l-b\"></div>");//清空左侧数据库列表

            }
        })

    }
    //点击分页
    function searchPageNew(n) {
        $('#pageIndexNow').val(n);
        searchResult(n);
    }
    //异步刷新左边的视图
    function refreshLeft() {
        $.ajax({
            type: "POST",
            url: "/MultiSearch/GetDateBaseList",
            success: function (msg) {
                $(".wrap-sideL").html("");
                $(".wrap-sideL").html(msg);
                $('.d-checkbox').attr('checked', 'checked');//左侧数据库列表全部选中
            },
            error: function (e) {

            }
        })
    }

    //异步刷新跨库分类
    function refreshUnionDB(classID) {

        var checked = [];
        $('.talkDivLi .f-checkbox:checkbox:checked').each(function () {
            checked.push($(this).val());
        }); //点击检索时取弹出框内勾选的数据库
        $("#checkDatabaseID").val(checked);
        var currentClassCode = $("#currentClassID").val();
        var checkDBID = $("#checkDatabaseID").val();
        $("#currentClassID").val(classID);

        if (currentClassCode != undefined && currentClassCode != "") {
            addCookie("ClassCode" + currentClassCode, checkDBID, 0);
        }
        var targetCheckDBID = getCookie("ClassCode" + classID);
        if (targetCheckDBID == undefined) {
            targetCheckDBID = "-1";
        }

        $.ajax({
            type: "GET",
            ansyc: true,
            url: "/MultiSearch/GetDBClass?targetClassID=" + classID + "&preClassID=" + currentClassCode + "&checkDBID=" + targetCheckDBID + "&type=Union",
            success: function (msg) {
                $(".talkDiv-body").html("");
                $(".talkDiv-body").html(msg);
                $("#classCode").val(classID);
                //firstload == 0;
            },
            error: function (e)
            { }
        })
    }
    $(document).keydown(function (event) {
        if (event.keyCode == 13) {
            searchResult(-1);
        }
    });
    $(function () {
        //页面初始化时，将首页选择的值存起来
        $("#beforeConditionValue").val($("#ConditionValue").val());
        $("#beforeConditionKey").val($("#ConditionKey").val());
        $('.d-checkbox').attr('checked', 'checked');//左侧数据库列表全部选中

        //页面初次加载时，默认异步加载当前分类对应的跨库数据
        //$('.wrap-r-t ul li').each(function (index, item) {
        //    if ($(item).attr("class") == "active") {
        //        firstload = 1;
        //        $(item).find("a").click();

        //    }
        //});

        searchResult(-1);

    })

    //切换排序方式
    function sortClick(sort, ename) {
        $('#pageIndexNow').val('1');
        $("#sortField").val(ename);
        if (sort == "desc") {
            $('#sortDescAsc').val('asc');
        }
        else {
            $('#sortDescAsc').val('desc');
        }
        searchResult(0);
    }
    //切换显示模式
    function formatList(formatstr) {
        if (formatstr == "list") {
            $('#listOrabst').val('list');
        }
        else {
            $('#listOrabst').val('abct');
        }
        searchResult(0);

    }
    ///全选
    function selectAll() {
        $('.d-checkbox').attr("checked", "checked");
    }
    ///清除
    function cancelAll() {
        $('.d-checkbox').removeAttr("checked", "checked");
    }
    ///检索
    function seachunion() {
        var checked = [];
        $('.d-checkbox:checkbox:checked').each(function () {
            checked.push($(this).val());
        });
        if (checked.length == 0) {
            messageAlert('请先选择数据库！');
            return;
        }
        databaseSelect();
    }
    //点击检索，触发检索事件异步刷新查询结果
    function databaseSelect() {
        var checked = [];
        $('.d-checkbox:checkbox:checked').each(function () {
            checked.push($(this).val());
        });
        $("#checkDatabaseID").val(checked);
        $.ajax({
            type: "POST",
            url: "/MultiSearch/Search",
            data: $('#formSearch').serialize(),
            beforeSend: function () { $("#loading").show(); },
            success: function (msg) {
                $("#reslultdiv").html("");
                $("#reslultdiv").html(msg);
                $("#loading").hide();
            },
            error: function (e)
            { $("#loading").hide(); }
        })
    }
    //点击检索，触发检索事件异步刷新查询结果
    function SingleDatabaseSelect(code) {
        
        $("#checkDatabaseID").val(code);
        $('.d-checkbox').each(function () {
            if ($(this).val() == code) {
                $(this).attr("checked", "checked");
            }
            else {
                $(this).removeAttr("checked", "checked");
            }
        });
        $.ajax({
            type: "POST",
            url: "/MultiSearch/Search",
            data: $('#formSearch').serialize(),
            beforeSend: function () { $("#loading").show(); },
            success: function (msg) {
                $("#reslultdiv").html("");
                $("#reslultdiv").html(msg);
                $("#loading").hide();
            },
            error: function (e)
            { $("#loading").hide(); }
        })
    }
</script>
@using CNKI.TPI.Web.Base
@using CNKI.TPI.Web.Search.Model
<div class="wrap">
    <!--面包屑一-->
    <div class="crumbLink">
        @*   <a href="javascript:void(0);">当前位置</a>&gt;<a href="javascript:void(0);">文献资源</a>&gt;期刊论文库*@
    </div>
    <div class="clearfix">
        <!-- wrap-sideL==START -->
        <div id="loadingleft" style="display: none; margin-left: 90px; margin-top: 70px; position: absolute">
            <img src="~/Images/loading.gif" width="50" />
        </div>
        <div class="wrap-sideL">
            @RenderPage("DataBaseList.cshtml")
        </div>
        <!-- wrap-sideL==END -->
        <!-- wrap-sideR==START -->
        <div class="wrap-sideR">
            <!--二级导航==开始-->
            <ul class="nav nav-tabs05 b0 clearfix">
                @if (ViewBag.UnionDBClassInfo != null && ViewBag.UnionDBClassInfo.Count > 0)
                {
                    foreach (CNKI.TPI.Web.Search.Model.DBCLASS item in ViewBag.UnionDBClassInfo)
                    {
                        string strclass = "";
                        if (@item.ClassCode == @ViewBag.UnionDBClassID)
                        {
                            strclass = "class=active";
                        }
                        <li @strclass><a href="javascript:void(0);" onClick="refreshUnionDB('@item.ClassCode')">@item.ClassName</a></li>
                    }
                }
            </ul>
            <input type="hidden" id="currentClassID" name="currentClassID" value="@ViewBag.UnionDBClassID" />
            <input type="hidden" id="SelectedDataBase" name="SelectedDataBase" value="@ViewBag.SelectedDataBase" />
            <!--二级导航==结束-->
            <form class="form form-h form-h-8em" id="formSearch">
                <fieldset>

                    <div class="f-gp clearfix" style="padding-left: 9px">
                        <span class="f-btn">
                            <a href="javascript:addCondition();" class='iconfont iconfont-add'></a>
                            <a href="javascript:delCondition();" class='iconfont iconfont-minus'></a>
                        </span>
                        <select id="ConditionKey" name="ConditionKey" class='f-select span2'>
                            @if (ViewBag.SeachInfo != null)
                            {
                                foreach (CNKI.TPI.Web.Search.Model.DBUSFIELD item in ViewBag.SeachInfo)
                                {
                                    if (@item.RealFieldName == @ViewBag.ConditionKey)
                                    {
                                        <option value="@item.RealFieldName" selected="selected">@item.ShowFieldName</option>
                                    }
                                    else
                                    {
                                        <option value="@item.RealFieldName">@item.ShowFieldName</option>
                                    }
                                }
                            }
                        </select>
                        <input id="ConditionValue" name="ConditionValue" type="text" maxlength="50" value="@ViewBag.SearchField" class="f-text span4">
                        <select id="SubConditionQuery" name="SubConditionQuery" class='f-select span1'>
                            @Html.Raw("<option value=\"2\" " + (ViewBag.ConditionType == ConditionType.Like ? "selected=\"selected\"" : "") + ">模糊</option>")
                            @Html.Raw("<option value=\"0\" " + (ViewBag.ConditionType == ConditionType.Eq ? "selected=\"selected\"" : "") + ">精确</option>")
                        </select>
                        <input class="btn btn-m btn-warn-f btn-submit" type="button" value="检索" onclick="searchResult(-1)" />
                        <a href="javascript:void(0);" class="dialog-p" style="font-size: 12px; margin-left: 0px">跨库检索范围</a>
                    </div>
                    <div class="f-gp clearfix" id="subCondition0" style="display: none">
                        <select id="SubConditionType0" name="SubConditionType0" class='f-select span1'>
                            <option value="0">并且</option>
                            <option value="1">或者</option>
                        </select>
                        <select id="SubConditionKey0" name="SubConditionKey0" class='f-select span2'>
                            @if (ViewBag.SeachInfo != null)
                            {
                                foreach (CNKI.TPI.Web.Search.Model.DBUSFIELD item in ViewBag.SeachInfo)
                                {
                            <option value="@item.RealFieldName">@item.ShowFieldName</option>
                                }
                            }
                        </select>
                        <input id="SubConditionValue0" name="SubConditionValue0" type="text" value="" class="f-text span4">
                        <select id="SubConditionQuery0" name="SubConditionQuery0" class='f-select span1'>
                            <option value="2">模糊</option>
                            <option value="0">精确</option>
                        </select>
                    </div>

                    @if (ViewBag.SearchLogID != -1 && ViewBag.SearchQuery.Count > 1)
                    {
                        IList<SearchQuery> searchQuery = ViewBag.SearchQuery;
                        for (int i = 1; i < searchQuery.Count; i++)
                        {
                            string subid = "subCondition" + i;
                            <div class="f-gp clearfix" id="@subid" style="padding-left:9px">
                                @Html.Raw("<select id=\"SubConditionType" + i + "\" name=\"SubConditionType" + i + "\" class='f-select span1'>")
                                @Html.Raw("<option value=\"0\" " + (searchQuery[i].Type == SearchMatchType.And ? "selected=\"selected\"" : "") + ">并且</option>")
                                @Html.Raw("<option value=\"1\" " + (searchQuery[i].Type == SearchMatchType.Or ? "selected=\"selected\"" : "") + ">或者</option>")
                                @Html.Raw("</select>")
                                @Html.Raw("<select id=\"SubConditionKey" + i + "\" name=\"SubConditionKey" + i + "\" class='f-select span2'>")
                                @foreach (var item in ViewBag.SeachInfo)
                                {
                                    if (item.RealFieldName == searchQuery[i].Key)
                                    {
                                        <option value="@item.RealFieldName" selected="selected">@item.ShowFieldName</option>
                                    }
                                    else
                                    {
                                        <option value="@item.RealFieldName">@item.ShowFieldName</option>
                                    }
                                }
                                @Html.Raw("</select>")
                                @Html.Raw("<input id=\"SubConditionValue" + i + "\" name=\"SubConditionValue" + i + "\" type=\"text\" value=\"" + searchQuery[i].Text + "\" class=\"f-text span4\" maxlength='50'>")
                                @Html.Raw("<select id=\"SubConditionQuery" + i + "\" name=\"SubConditionQuery" + i + "\" class='f-select span1' autocomplete=\"off\">")
                                @Html.Raw("<option value=\"2\" " + (searchQuery[i].ConditionType == ConditionType.Like ? "selected=\"selected\"" : "") + ">模糊</option>")
                                @Html.Raw("<option value=\"0\" " + (searchQuery[i].ConditionType == ConditionType.Eq ? "selected=\"selected\"" : "") + ">精确</option>")
                                @Html.Raw("</select>")
                            </div>
                        }
                    }
                    <input type="hidden" id="beforeQueryValue" name="beforeQueryValue" value="" />
                    <input type="hidden" id="beforeConditionValue" name="beforeConditionValue" value="" />
                    <input type="hidden" id="beforeConditionKey" name="beforeConditionKey" value="" />
                    @{
                        int currentSubid = 0;
                        if (ViewBag.SearchLogID != -1 && ViewBag.SearchQuery.Count > 1)
                        {
                            currentSubid = ViewBag.SearchQuery.Count - 1;
                        }
                    }
                    <input type="hidden" id="currentSubid" name="currentSubid" value="@currentSubid" />
                    <input type="hidden" id="checkDatabaseID" name="checkDatabaseID" value="" />
                    <input type="hidden" id="pageIndexNow" name="pageIndexNow" value="1" />
                    <input type="hidden" id="sortField" name="sortField" value="" />
                    <input type="hidden" id="sortDescAsc" name="sortDescAsc" value="" />
                    <input type="hidden" id="listOrabst" name="listOrabst" value="" />
                    <input type="hidden" id="classCode" name="classCode" value="@ViewBag.UnionDBClassID" />
                    <div id="beforeContion" style="display: none"></div>
                </fieldset>

            </form>

            <!-- 弹窗 -->
            <!--talkDiv弹出层(不带图片，宽668)-->
            <div class="talkDiv talkW-668 hidden">
                <div class="talkDiv-title">
                    <a class="icon icon-close" title="关闭" href="javascript:void(0);"><i class="iconfont icon-error3"></i></a>
                </div>
                <div class="talkDiv-body">
                    <ul>
                        @if (@ViewBag.SeachDataBase != null)
                        {
                            string selected = "";
                            if (ViewBag.SelectedDataBase != null)
                            {
                                selected = "," + ViewBag.SelectedDataBase + ",";//加逗号，防止判断时ID出现部分叠加重复情况
                            }
                            @Html.Raw("<li class=\"talkDivLi\">")
                            @Html.Raw("<div class=\"talkDivLiT\" style=\"font-size: 14px\">数据库列表</div>")
                            foreach (CNKI.TPI.Web.Search.Model.DBCONFIG item in ViewBag.SeachDataBase)
                            {
                                string strcheck = "";
                                if (selected.IndexOf("," + @item.DbID.ToString() + ",") > -1)//已经在首页选中，则默认选中
                                {
                                    strcheck = "checked=checked";
                                }
                                @Html.Raw("<a  href=\"javascript:void(0)\"><label style=\"font-size:12px;margin:0px;\" class=\"f-label col-md-3 text-ellipsis\"><input type=\"checkbox\" " + @strcheck + " value=" + item.DbID + " class=\"f-checkbox\" />" + item.CNName + "</label></a>")


                            }
                            @Html.Raw("</li>");
                        }
                    </ul>
                </div>
                <div class="talkDiv-btn">
                    <a href="javascript:void(0);" class="btn btn-m btn-submit btn-warn-f select-all">全选</a>
                    &#12288;<a href="javascript:void(0);" class="btn btn-m btn-submit btn-prim-f cancel">取消</a>
                </div>

            </div>

            <div id="loading" style="display: none; margin-left: 300px; margin-top: 50px; position: absolute">
                <img src="~/Images/loading.gif" width="50" />
            </div>
            <div id="reslultdiv">
                @*@RenderPage("ResultList.cshtml")*@
            </div>
            @*   <div class="xg-search">
                    <h2><span>相关搜索</span></h2>
                    <div class="xg-search-con">
                        <a href="">管理知识</a>
                        <a href="">知识人员</a>
                        <a href="">管理知识</a>
                        <a href="">战略管理</a>
                        <a href="">知识资源管理</a>
                        <a href="">知识资本管理</a>
                        <a href="">隐性知识</a>
                        <a href="">知识共享</a>
                        <a href="">显性知识</a>
                        <a href="">知识创新</a>
                        <a href="">知识经济时代</a>
                        <a href="">知识管理系统</a>
                        <a href="">知识管理能力</a>
                        <a href="">知识管理战略</a>
                        <a href="">知识管理模式</a>
                        <a href="">知识管理体系</a>
                    </div>
                </div>*@
        </div>
        <!-- wrap-sideR==END -->


    </div>

</div>

<script>
    //相似词检索
    $("#ConditionValue").keyup(smartKeyup);

</script>
